package com.fengxing.ams.web.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fengxing.ams.pub.util.RelativeDateFormat;
import com.fengxing.ams.web.action.view.NotificationViewVO;
import com.fengxing.ams.web.action.view.ResponseVO;
import com.fengxing.ams.web.common.IContextManager;
import com.fengxing.ams.web.facade.intf.INotificationFacade;
import com.fengxing.ams.web.facade.mouble.notification.NotificationVO;
import com.fengxing.ams.web.facade.mouble.user.UserVO;

@Controller
@RequestMapping("notification")
@Transactional
public class NotificationAction {
	
	private Log logger = LogFactory.getLog(NotificationAction.class);
	@Autowired
	private IContextManager contextManager;
	@Autowired
	private INotificationFacade notificationFacade;
	
	@RequestMapping(value = "/unreadQuery.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO unreadQuery() {
		ResponseVO vo = new ResponseVO();
		//权限检查
		UserVO loginedUser = this.contextManager.getUserVO();
		if (loginedUser == null) {
			vo.setMessage("请重新登录或刷新页面！");
			return vo;
		}

		//使用email，到学生表中查询信息
		Boolean isHaving = this.notificationFacade.queryUnread();
		NotificationViewVO view = new NotificationViewVO();
		view.setHavingUnread(isHaving);
		vo.setData(view);
		vo.setMessage("请求成功！");
		return vo;
	}
	
	@RequestMapping(value = "/list.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO unreadList() {
		ResponseVO vo = new ResponseVO();
		//权限检查
		UserVO loginedUser = this.contextManager.getUserVO();
		if (loginedUser == null) {
			vo.setMessage("请重新登录或刷新页面！");
			return vo;
		}

		//使用email，到学生表中查询信息
		List<NotificationVO> list = this.notificationFacade.queryUnreadList();
		for (NotificationVO notification : list) {
			notification.setDescTime(RelativeDateFormat.format(notification.getCreatedDate()));
		}
		vo.setData(list);
		vo.setMessage("请求成功！");
		return vo;
	}
	
	@RequestMapping(value = "/markRead.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO markRead(@RequestBody NotificationViewVO param) {
		ResponseVO vo = new ResponseVO();
		//权限检查
		UserVO loginedUser = this.contextManager.getUserVO();
		if (loginedUser == null) {
			vo.setMessage("请重新登录或刷新页面！");
			return vo;
		}

		//使用email，到学生表中查询信息
		this.notificationFacade.markRead(param.getIds());
		vo.setMessage("请求成功！");
		return vo;
	}
	
	//查询自登录以后或上次查询之后有没有新的通知
	@RequestMapping(value = "/newQuery.do", method = RequestMethod.POST)
	public @ResponseBody ResponseVO newQuery() {
		ResponseVO vo = new ResponseVO();
		//权限检查
		UserVO loginedUser = this.contextManager.getUserVO();
		if (loginedUser == null) {
			vo.setMessage("请重新登录或刷新页面！");
			return vo;
		}

		//使用email，到学生表中查询信息
		List<NotificationVO> vos = this.notificationFacade.queryNew();
		for (NotificationVO vo1 : vos) {
			vo1.setDescTime(RelativeDateFormat.format(vo1.getCreatedDate()));
		}
		vo.setData(vos);
		vo.setMessage("请求成功！");
		return vo;
	}
	
}
